package com.ztgf.shopping.api.cms.epg;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.epg.EpgShowWindowBO;
import com.ztgf.shopping.models.bo.epg.EpgShowWindowSaveBO;
import com.ztgf.shopping.models.dto.epg.EpgShowWindowDTO;
import com.ztgf.shopping.models.service.epg.IEpgShowWindowService;
import com.ztgf.shopping.models.vo.epg.EpgShowWindowVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * author: chenyu.wang
 * date: 2020-05-19 11:35
 * Description: 橱窗（新鲜好物）控制器
 */
@RestController
@RequestMapping("/cms/epgShowWindow")
@Slf4j
@Api(value = "EpgShowWindowController", tags = "橱窗（新鲜好物）接口")
public class EpgShowWindowController {

    @DubboReference
    private IEpgShowWindowService showWindowService;

    /**
     * 橱窗（新鲜好物） 根据功能id
     * @param moduleId 功能id
     * @return
     */
    @ApiOperation(value = "橱窗（新鲜好物）列表 根据功能id",notes = "橱窗（新鲜好物）列表 根据功能id")
    @PostMapping("/getListByModuleId")
    public Result<List<EpgShowWindowVO>> getListByModuleId(Long moduleId){
        EpgShowWindowBO epgShowWindowBO = new EpgShowWindowBO();
        epgShowWindowBO.setModuleId(moduleId);
        List<EpgShowWindowVO> epgShowWindowVOList = showWindowService.getBySelectShowWindowInfoNoPage(epgShowWindowBO);
        return new Result<>(epgShowWindowVOList);
    }

    @PostMapping(value = "/insert")
    @ApiOperation(value = "添加橱窗表", notes = "添加橱窗表")
    public Result insert( EpgShowWindowSaveBO showWindowSaveBO) {
        boolean flag = showWindowService.add(showWindowSaveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改橱窗表", notes = "修改橱窗表")
    public Result update( EpgShowWindowSaveBO showWindowSaveBO) {
        EpgShowWindowDTO epgShowWindowDTO = new EpgShowWindowDTO();
        BeanUtils.copyProperties(showWindowSaveBO, epgShowWindowDTO);
        epgShowWindowDTO.setId(showWindowSaveBO.getId());
        boolean flag = showWindowService.updateById(epgShowWindowDTO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除橱窗表", notes = "删除橱窗表")
    @ApiImplicitParam(name = "id", value = "字典表id", required = true, paramType = "query", dataType = "Long")
    public Result delete(@RequestParam Long id) {
        boolean flag = showWindowService.deleteById(id);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除橱窗表", notes = "批量删除橱窗表")
    @ApiImplicitParam(name = "ids", value = "ids:以逗号隔开", required = true, paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam String ids) {
        if (StrUtils.isEmpty(ids)) {
            return new Result(CodeEnum.FAIL_100002);
        }
        String[] arr = ids.split(",");
        showWindowService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/getById")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<EpgShowWindowDTO> getById(@RequestParam Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return new Result<>(showWindowService.selectById(id));
    }

    @ApiOperation(value = "橱窗排序", notes = "橱窗排序")
    @PostMapping("/querySelectPageList")
    public Result<Page<EpgShowWindowVO>> querySelectPageList(EpgShowWindowBO bo) {
        if(null == bo)
            return new Result<>(CodeEnum.FAIL_100002 );
        Page<EpgShowWindowVO> page = showWindowService.getBySelectShowWindowInfoPage(bo);
        return new Result<>(page);
    }
}
